package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.domain.Message;
import com.yixin.hospital.entity.vo.MessageVO;
import com.yixin.hospital.entity.vo.UserMessageTypeVO;
import com.yixin.hospital.enums.MessageStatusEnum;
import com.yixin.hospital.enums.MessageTypeEnum;
import com.yixin.hospital.mapper.MessageMapper;
import com.yixin.hospital.service.MessageService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【message(消息通知表)】的数据库操作Service实现
* @createDate 2024-05-23 15:12:39
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Override
    public ApiResult<?> userMessageType(Long userId) {
        List<Message> list = this.list(new LambdaQueryWrapper<Message>().select(Message::getMessageType).eq(Message::getUserId, userId).groupBy(Message::getMessageType));
        List<UserMessageTypeVO> userMessageTypeVOList = new ArrayList<>();
        for (Message message : list) {
            UserMessageTypeVO userMessageTypeVO = new UserMessageTypeVO();
            userMessageTypeVO.setMessageType(message.getMessageType());
            userMessageTypeVO.setDesc(MessageTypeEnum.getDesc(message.getMessageType()));
            userMessageTypeVOList.add(userMessageTypeVO);
        }
        return ApiResult.success(userMessageTypeVOList);
    }

    @Override
    public ApiResult<?> userMessageByType(int currentPage, int pageSize, String messageType, Long userId) {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<Message>().eq(Message::getUserId, userId);
        if (Strings.isNotBlank(messageType)) wrapper.eq(Message::getMessageType, messageType);
        Page<Message> page = this.page(Page.of(currentPage, pageSize), wrapper);
        PageInfo<MessageVO> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(page, pageInfo);
        pageInfo.setRecords(BeanUtil.copyToList(page.getRecords(), MessageVO.class));
        for (MessageVO record : pageInfo.getRecords()) {
            record.setMessageTypeName(MessageTypeEnum.getDesc(record.getMessageType()));
            record.setStatusName(MessageStatusEnum.getDesc(record.getStatus()));
        }
        return ApiResult.success(pageInfo);
    }

    @Override
    public ApiResult<?> messageRead(Long id, String name) {
        Message message = new Message();
        message.setId(id);
        message.setStatus(1);
        message.setUpdatedBy(name);
        this.updateById(message);
        return ApiResult.success();
    }

    @Override
    public ApiResult<?> messageAllRead(Long userId) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getUserId, userId);
        List<Message> list = this.list(queryWrapper);
        for (Message message : list) {
            message.setStatus(1);
        }
        this.updateBatchById(list);
        return ApiResult.success();
    }
}




